package DailyPractice.Day13;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 跳跃台阶
 * @author lumin
 * @date 2022/05/09 10:35
 **/

public class JumpSlate {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] res = new int[M + 1];
        for (int i = N + 1; i <= M; i++) {
            //初始化大小
            res[i] = Integer.MAX_VALUE;
        }
        for (int cur = N; cur < M; cur++) {
            //如果当前位置的值等于初始值，表示不可以到达此位置
            if (res[cur] == Integer.MAX_VALUE) {
                continue;
            }
            ArrayList<Integer> list = fac(cur);//存储cur的所有因子
            for (int j = 0; j < list.size(); j++) {
                int temp = cur + list.get(j);//当前可以到达的位置
                int count = res[cur] + 1;//需要的步数
                if (temp <= M && count < res[temp]) {
                    res[temp] = count;
                }
            }
        }
        int s = res[M];
        if (s == Integer.MAX_VALUE) {
            System.out.println(-1);
            return;
        }
        System.out.println(s);
    }

    //查找因子
    public static ArrayList<Integer> fac(int n) {
        ArrayList list = new ArrayList();
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                list.add(i);
                if (i != n / i) {
                    list.add(n / i);
                }
            }
        }
        return list;
    }
}
